<?php
if($_SESSION['index']=='')
{
	displayError('Une erreur interne s\'est produit (perte du fichier index.xml)');
}
$link = @mysql_connect($_SESSION['conf_db']['host'], $_SESSION['conf_db']['login'], $_SESSION['conf_db']['password']);
$error = '';
if($link===false)
{
	$error  .= '<span class="install_message_error">Impossible de se connecter au server de données...<br><i>'.mysql_error().'</i></span>';
}
else 
{
	if(!mysql_select_db($_SESSION['conf_db']['database'], $link))
	{
		$error  .= '<span class="install_message_error">Impossible de séléctionner la base...<br><i>'.mysql_error().'</i></span>';
	}
}
if($error!='')
{
?>
document.getElementById('install_content_data').innerHTML = '<span class="install_message_error"><?php echo strToJs($error)?></span>';
document.getElementById('install_next').disabled=true;
<?php
	die();		
}
function createDir($path)
{
	global $current_path;
	$message = '';
	if(!@mkdirTree($current_path, $path))
	{
		$message = $file_name.' : Could not create the directory';
	}
	if($message!='')
	{
		$message = 'Erreur lors de l\'extraction de l\'archive<br><br>'.implode('<br>', $errors);
		displayButtons();
?>
document.getElementById('install_content_data').innerHTML = '<span class="install_message_error"><?php echo strToJs($message)?></span>';
document.getElementById('install_next').disabled=true;
<?php
		die();
	}
}
function createFile($file_name, $data)
{
	global $current_path;
	$message = '';
	createDir(dirname($file_name));
	if(!@file_put_contents($current_path.$file_name, $data))
	{
		$message = $file_name.' : Could not open for writing';
	}
	if($message!='')
	{
		$message = 'Erreur lors de l\'extraction de l\'archive<br><br>'.$message;
		displayButtons();
?>
document.getElementById('install_content_data').innerHTML = '<span class="install_message_error"><?php echo strToJs($message)?></span>';
document.getElementById('install_next').disabled=true;
<?php
		die();
	}
}
function executeSql($query)
{
	global $link;
	mysql_query($query, $link);
	$message = mysql_error($link);
	if($message!='')
	{
		$message = 'Erreur lors de l\'extraction de l\'archive<br><br>'.$message;
		displayButtons();
?>
document.getElementById('install_content_data').innerHTML = '<span class="install_message_error"><?php echo strToJs($message)?></span>';
document.getElementById('install_next').disabled=true;
<?php
		die();
	}
}

$setup = new SimpleXMLElement($_SESSION['index']);
if(count($setup->scripts)>0)
{
	$search = array('«conf_db_host»', '«conf_db_login»', '«conf_db_password»', '«conf_db_database»', '«conf_db_prefix»', '«INSTALL_PACKAGE_HOST»', '«conf_root_login»', '«conf_root_password»', '«conf_root_password_md5»');
	$replace = array(addslashes($_SESSION['conf_db']['host']), addslashes($_SESSION['conf_db']['login']), addslashes($_SESSION['conf_db']['password']), addslashes($_SESSION['conf_db']['database']), addslashes($_SESSION['conf_db']['prefix']), addslashes(INSTALL_PACKAGE_HOST), addslashes($_SESSION['conf_root']['login']), addslashes($_SESSION['conf_root']['password']), addslashes(md5($_SESSION['conf_root']['password'])));
	foreach ($setup->scripts[0]->script as $script)
	{
		if($script['event']=='aftercopy')	
		{
			switch($script['type'])
			{
				case 'mkdir':
					createDir($script['path']);
					break;
				case 'mkfile':
					createFile($script['path'], trim(str_replace($search, $replace, ''.$script)));
					break;
				case 'sql':
					executeSql(str_replace($search, $replace, ''.$script));
					break;
			}
		}
	}
}

file_put_contents($current_path.'/install_report.php5', '<?php
$install_report=array(
	\'application_name\' => \''.INSTALL_APPLICATION_NAME.'\'
	, \'application_version\' => \''.addslashes($setup->application[0]->version[0]).'\'
	, \'packages_host\' => \''.INSTALL_PACKAGE_HOST.'\'
	, \'database_host\' => \''.addslashes($_SESSION['conf_db']['host']).'\'
	, \'database_database\' => \''.addslashes($_SESSION['conf_db']['database']).'\'
	, \'database_login\' => \''.addslashes($_SESSION['conf_db']['login']).'\'
	, \'database_password\' => \''.addslashes($_SESSION['conf_db']['password']).'\'
	, \'database_prefix\' => \''.addslashes($_SESSION['conf_db']['prefix']).'\'
	, \'root_login\' => \''.addslashes($_SESSION['conf_root']['login']).'\'
	, \'root_password\' => \''.addslashes(md5($_SESSION['conf_root']['password'])).'\'
	);
?>');

$_SESSION['end'] = true;
cleanExtracted();
?>
document.getElementById('install_content_intro').innerHTML = 'L\'Installation est terminée. Vous pouvez lancer l\'application';
document.getElementById('install_content_data').innerHTML = ''
document.getElementById('install_back').style.display='none'
document.getElementById('install_next').value='Terminer'
document.getElementById('install_next').style.display='block'

document.getElementById('install_next').onclick = function()
{
	top.location.href = "<?php echo $setup->application[0]->startpath[0]?>";
}